package flextv.main.command {

import com.adobe.cairngorm.commands.ICommand;
import com.adobe.cairngorm.control.CairngormEvent;

import flash.events.Event;
import flash.filesystem.File;

import flextv.core.logic.Logger;
import flextv.core.logic.Util;
import flextv.core.vo.StoredItem;
import flextv.main.event.SaveVideoFileEvent;

import mx.logging.ILogger;

public class SaveVideoFileCommand implements ICommand {
	
	private static const LOG: ILogger = Logger.getLog(SaveVideoFileCommand);
	
	public function SaveVideoFileCommand() {
	}

	public function execute(_event: CairngormEvent): void {
		const event: SaveVideoFileEvent = _event as SaveVideoFileEvent;
		LOG.info("Executing [event=" + event + "].");
		
		const item: StoredItem = event.item;
		
		const targetFile: File = new File();
		targetFile.addEventListener(Event.SELECT, function(event: Event): void {
			
			const name: String = targetFile.name;
			var targetPath: String = targetFile.nativePath;
			// append *.flv
			if(name.length < 5) {
				targetPath += ".flv";
			} else {
				const extension: String = name.substr(name.length - 4, 4).toLowerCase();
				if(extension != ".flv") {
					targetPath += ".flv";
				}
			}
			
			LOG.info("Saving FLV file for item ["+item+"] to ["+targetPath+"]");
			const sourceFlvFile: File = Util.resolveFlvFile(item);
			sourceFlvFile.copyToAsync(new File(targetPath), true); // overwrite == true
		});
		targetFile.browseForSave("Enter target for the video");
		
		
	}
	
}
}